package com.cloudOffice.controller;

import com.cloudOffice.result.Result;
import com.cloudOffice.service.SysUserService;
import com.cloudOffice.storage.CommonStorage;
import com.cloudOffice.vo.system.LoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: cloud-office
 * @className: LoginController
 * @description: 后台登录登出
 * @data: 2024/2/7 16:11
 * @author: ihu
 * @version: 1.0
 **/
@Slf4j
@Tag(name = "账户后台管理")
@RestController("LoginController")
@RequestMapping("/admin/system/index")
public class LoginController {
	
	@Resource
	private SysUserService sysUserService;
	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	
	/**
	 * 登录
	 */
	@PostMapping("login")
	@Operation(summary = "登录")
	public Result<Map<String, Object>> login(@RequestBody LoginVo loginVo) {
		log.info("登录：loginVo: {}", loginVo);
		return Result.success(sysUserService.login(loginVo));
	}
	
	/**
	 * 获取用户信息
	 */
	@GetMapping("info")
	@Operation(summary = "获取用户信息")
	public Result<Map<String, Object>> info() {
		log.info("获取用户信息：username: {}", CommonStorage.getUsername());
		return Result.success(sysUserService.getUserInfo(CommonStorage.getUsername()));
	}
	
	/**
	 * 退出
	 */
	@PostMapping("logout")
	@Operation(summary = "退出")
	public Result<?> logout() {
		log.info("再次获取token：id: {},username: {}", CommonStorage.getId(), CommonStorage.getUsername());
		// 在redis中删除当前用户的token信息
		redisTemplate.delete(CommonStorage.getId() + CommonStorage.getUsername());
		return Result.success();
	}
	
	@GetMapping("/againGetToken")
	@Operation(summary = "再次获取token")
	public Result<Map<String, Object>> againGetToken() {
		log.info("再次获取token：id: {},username: {}", CommonStorage.getId(), CommonStorage.getUsername());
		return Result.success(sysUserService.againGetToken(CommonStorage.getId(), CommonStorage.getUsername()));
	}
	
}
